package main

import (
	"fmt"
	"time"
)

func main() {
	var t = time.Now()
	fmt.Printf("%T, %[1]v\n", t)
	fmt.Printf("%T,%[1]v\n", t.UTC())
	//时间格式化
	fmt.Println(t.Format("0102 150405 06 pm -0700"))
	fmt.Println(t.UTC().Format("0102 150405 06 pm"))
	fmt.Println(t.Local().Format("2006/01/02 15:04:05 -0700"))
	//时间解析，理解UTC，实例1
	timeStr := "2023#02/17\t14$50:23.123" // Parse, 必须和时间字符串对应，否则解析失败
	t2, err := time.Parse("2006#01/02\t15$04:05.000", timeStr)
	if err != nil {
		fmt.Println(err, "@11111@")
		panic(err)
	}
	fmt.Println("success", t2, t2.Format("2006"))
	fmt.Println(t2.Location())
	fmt.Println(t2.Local())
	fmt.Println("local time:", t2.Local().Format("2006/01/02 15:04:05 -0700"))
	// 时间解析，实例2
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		panic(err)
	}
	t3, err := time.ParseInLocation("2006#01/02\t15$04:05.000", timeStr, loc)
	if err != nil {
		panic(err)
	}
	fmt.Println("success", t3, t3.Format("2006/01/02"))
	fmt.Println(t3.UTC().Format("2006/01/02 15:04:05 -0700"))

	fmt.Println(
		t.Year(), t.Month(), t.Month().String(), int(t.Month()), t.Day(),
		t.YearDay(),
		t.Hour(), t.Minute(), t.Second(),
		t.Nanosecond(),
		"\n",
		t.Unix(), t.UnixMilli(), t.UnixMicro(), t.UnixNano(),
	)
	fmt.Println(t.Date())
	fmt.Println(t.ISOWeek())

	// 时间增量
	t4 := time.Now()
	delta := t4.Sub(t3)
	fmt.Println(delta, delta.Hours(), delta.Seconds(), delta.String())
	fmt.Printf("delta:%v,%[1]T\n", delta)

	d1 := time.Duration(3) // 3ns
	fmt.Println("d1=", d1)
	fmt.Println(time.Duration(3*time.Second), time.Duration(3*time.Hour))
	t5 := t3.Add(time.Duration(3 * time.Second))
	fmt.Println("t5=", t5)
	t6 := t3.Add(-time.Duration(3 * time.Second))
	fmt.Println("t6=", t6)
	fmt.Println(t3.After(t5))
	fmt.Println(t3.After(t6))
}
